﻿using System.Data;
using System.Data.Odbc;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System;
using Wuqi.Webdiyer;

namespace ZLDC.Common
{

    public class CmnFunc
    {
        public CmnFunc()
        {
        }

        public static string getMoneyMask(string valMoney)
        {
            string TmpVal = "";
            string RetVal = "";

            TmpVal = valMoney;
            TmpVal = TmpVal.Trim();
            if (TmpVal != "")
            {
                if (TmpVal.IndexOf(",") >= 0)
                {
                    RetVal += TmpVal.Substring(0, TmpVal.IndexOf(","));
                    RetVal += getMoneyMask(TmpVal.Substring(TmpVal.IndexOf(",") + 1, TmpVal.Length - TmpVal.IndexOf(",") - 1));
                }
                else RetVal = TmpVal;
            }
            else RetVal = "0.00";
            if (!CmnFunc.IsNumber(RetVal))
                RetVal = "0.00";

            return RetVal;
        }


        public static string GetPYString(string str)
        {
            string tempStr = "";
            foreach (char c in str)
            {
                if ((int)c >= 33 && (int)c <= 126)
                {
                    tempStr += c.ToString();
                }
                else
                {
                    tempStr += GetPYChar(c.ToString());
                }
            }
            return tempStr;
        }

        public static bool IsNumber(string sInput)
        {
            for (int i = 0; i < sInput.Length; i++)
            {
                if ((sInput[i] > '9' || sInput[i] < '0') && sInput[i] != '.' && sInput[i] != '-')
                    return false;
            }
            return true;
        }


        public static bool FormateBetweenDate(string sDt, out string pDtBegin, out string pDtEnd)
        {
            string sDtBegin = "";
            string sDtEnd = "";
            try
            {
                if (sDt.Length == 4)
                {
                    sDtBegin = sDt + "0101";
                    sDtEnd = sDt + "1231";
                }
                if (sDt.Length == 6)
                {
                    sDtBegin = sDt + "01";
                    DateTime dt = DateTime.Parse(sDtBegin.Substring(0, 4) + "-" + sDtBegin.Substring(4, 2) + "-" + sDtBegin.Substring(6, 2)
                        , null, System.Globalization.DateTimeStyles.AdjustToUniversal);
                    dt = dt.AddMonths(1);
                    dt = dt.AddDays(-1);
                    sDtEnd += dt.ToString("yyyyMMdd");
                }
                if (sDt.Length == 8)
                {
                    sDtBegin = sDtEnd = sDt;
                }
                sDtBegin = sDtBegin.Substring(0, 4) + "-" + sDtBegin.Substring(4, 2) + "-" + sDtBegin.Substring(6, 2);
                sDtEnd = sDtEnd.Substring(0, 4) + "-" + sDtEnd.Substring(4, 2) + "-" + sDtEnd.Substring(6, 2);
                sDtBegin += " 00:00:00";
                sDtEnd += " 23:59:59";
            }
            catch
            {
                pDtBegin = sDtBegin;
                pDtEnd = sDtEnd;
                return false;
            }
            pDtBegin = sDtBegin;
            pDtEnd = sDtEnd;
            return true;
        }

        public static bool IsTime(string sIn)
        {
            sIn = sIn.Trim();
            int nLen = sIn.Length;
            if (nLen < 4)
                return false;
            int nPos = sIn.IndexOf(":");
            if (nPos <= 0 || nPos == nLen)
                return false;
            string sHour = sIn.Substring(0, nPos);
            if (sHour.Length > 2)
                return false;
            string sMinute = sIn.Substring(nPos + 1, nLen - nPos - 1);
            if (sMinute.Length != 2)
                return false;
            sHour = sHour.PadLeft(2, '0');
            if (string.Compare(sHour, "00") < 0)
                return false;
            if (string.Compare(sHour, "23") > 0)
                return false;
            if (string.Compare(sMinute, "00") < 0)
                return false;
            if (string.Compare(sMinute, "59") > 0)
                return false;
            return true;
        }

        public static string per15To18(string perIDSrc)
        {
            int iS = 0;

            int[] iW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            string LastCode = "10X98765432";
            string perIDNew;

            perIDNew = perIDSrc.Substring(0, 6);
            perIDNew += "19";

            perIDNew += perIDSrc.Substring(6, 9);

            for (int i = 0; i < 17; i++)
            {
                iS += int.Parse(perIDNew.Substring(i, 1)) * iW[i];
            }

            int iY = iS % 11;
            perIDNew += LastCode.Substring(iY, 1);

            return perIDNew;
        }


        public static string per18To15(string perIDSrc)
        {
            return perIDSrc.Substring(0, 6) + perIDSrc.Substring(8, 9);
        }

        public static bool CheckIDCard(string Id)
        {
            if (Id.Length == 18)
            {
                bool check = CheckIDCard18(Id);
                return check;
            }
            else if (Id.Length == 15)
            {
                bool check = CheckIDCard15(Id);
                return check;
            }
            else
            {
                return false;
            }
        }

        public static string getBirthDay(string Id, bool bLong)
        {
            string sDT = "";
            if (Id.Length == 18)
            {
                sDT = Id.Substring(6, 8);
            }
            else if (Id.Length == 15)
            {
                sDT = "19" + Id.Substring(6, 6);
            }
            if (sDT != "" && bLong)
                sDT = sDT.Substring(0, 4) + "-" + sDT.Substring(4, 2) + "-" + sDT.Substring(6, 2);
            return sDT;
        }



        private struct WIN32_FIND_DATA { public int dwFileAttributes; public int ftCreationTime_dwLowDateTime; public int ftCreationTime_dwHighDateTime; public int ftLastAccessTime_dwLowDateTime; public int ftLastAccessTime_dwHighDateTime; public int ftLastWriteTime_dwLowDateTime; public int ftLastWriteTime_dwHighDateTime; public int nFileSizeHigh; public int nFileSizeLow; public int dwReserved0; public int dwReserved1; [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 260)] public string cFileName; [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 14)] public string cAlternateFileName; }
        [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        private static extern IntPtr FindFirstFile(string pFileName, ref WIN32_FIND_DATA pFindFileData);
        [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        private static extern bool FindNextFile(IntPtr hndFindFile, ref WIN32_FIND_DATA lpFindFileData);
        [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool FindClose(IntPtr hndFindFile); private static long ToLong(int height, int low) { long v = (uint)height; v = v << 0x20; v = v | ((uint)low); return v; }
        public static long GetFileSize(string sFile)
        {
            long nSize = 0;
            WIN32_FIND_DATA findFileData = new WIN32_FIND_DATA();
            FindFirstFile(sFile, ref findFileData);
            nSize = ToLong(findFileData.nFileSizeHigh, findFileData.nFileSizeLow);
            return nSize;
        }

        public static void setCustInfoOfAspnet(AspNetPager aspnetpager)
        {
            if (aspnetpager.RecordCount > 0)
            {
                aspnetpager.Visible = true;
                aspnetpager.CustomInfoHTML = "<span style='color:#666666'>总条数：</span><span style='color:Bule'><strong>" + aspnetpager.RecordCount.ToString() + "</strong></span>&nbsp;&nbsp;";
                aspnetpager.CustomInfoHTML += "<span style='color:#666666'>总页数：</span><span style='color:Bule'><strong>" + aspnetpager.PageCount.ToString() + "</strong></span>&nbsp;&nbsp;";
                aspnetpager.CustomInfoHTML += "<span style='color:#666666'>当前页：</span><span style='color:Red'><strong>" + aspnetpager.CurrentPageIndex.ToString() + "</strong></span>";
            }
            else
            {
                aspnetpager.Visible = false;
            }
        }

        #region 私有函数
        private static bool CheckIDCard18(string Id)
        {
            long n = 0;
            if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;
            }
            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = Id.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
            {
                return false;
            }
            return true;
        }
        private static bool CheckIDCard15(string Id)
        {
            long n = 0;
            if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
            {
                return false;
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;
            }
            string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;
            }
            return true;
        }
        private static string GetPYChar(string c)
        {
            byte[] array = new byte[2];
            array = System.Text.Encoding.Default.GetBytes(c);
            int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));

            if (i < 0xB0A1) return "*";
            if (i < 0xB0C5) return "a";
            if (i < 0xB2C1) return "b";
            if (i < 0xB4EE) return "c";
            if (i < 0xB6EA) return "d";
            if (i < 0xB7A2) return "e";
            if (i < 0xB8C1) return "f";
            if (i < 0xB9FE) return "g";
            if (i < 0xBBF7) return "h";
            if (i < 0xBFA6) return "g";
            if (i < 0xC0AC) return "k";
            if (i < 0xC2E8) return "l";
            if (i < 0xC4C3) return "m";
            if (i < 0xC5B6) return "n";
            if (i < 0xC5BE) return "o";
            if (i < 0xC6DA) return "p";
            if (i < 0xC8BB) return "q";
            if (i < 0xC8F6) return "r";
            if (i < 0xCBFA) return "s";
            if (i < 0xCDDA) return "t";
            if (i < 0xCEF4) return "w";
            if (i < 0xD1B9) return "x";
            if (i < 0xD4D1) return "y";
            if (i < 0xD7FA) return "z";

            return "*";
        }
        private static char FullCodeToHalfCode(char c)
        {

            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(c.ToString());

            int H = Convert.ToInt32(bytes[1]);
            int L = Convert.ToInt32(bytes[0]);

            int value = H * 256 + L;


            if (value >= 65281 && value <= 65374)
            {
                int halfvalue = value - 65248;
                byte halfL = Convert.ToByte(halfvalue);

                bytes[0] = halfL;
                bytes[1] = 0;
            }
            else if (value == 12288)
            {
                int halfvalue = 32;
                byte halfL = Convert.ToByte(halfvalue);

                bytes[0] = halfL;
                bytes[1] = 0;
            }
            else
            {
                return c;
            }


            string ret = System.Text.Encoding.Unicode.GetString(bytes);

            return Convert.ToChar(ret);
        }
        #endregion

        public static string removeRightComma(string str)
        {
            if (str.EndsWith(","))
            {
                str = str.Remove(str.Length - 1, 1);
                removeRightComma(str);
            }
            return str;
        }
    }
}
